package com.fruit.controller.app.edition;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fruit.controller.base.BaseController;
import com.fruit.pojo.common.Page;
import com.fruit.pojo.version.AppVersion;
import com.fruit.service.edition.AppVersionManager;
import com.fruit.util.ResultInfo;

@Controller
@RequestMapping(value = "/app/edition/")
public class EditionController extends BaseController {
	
	@Resource(name = "appVersionService")
	private AppVersionManager appVersionService;

	@RequestMapping(value = "versionInfo", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> appVersionInfo(Page page,HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "查询版本信息");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			Map<String, Object> params = new HashMap<String, Object>();
			String appType = request.getParameter("appType");
			int start = (page.getPage()-1) * page.getShowNumber();
			params.put("appType", appType);
			params.put("start", start);
			params.put("showNumber", page.getShowNumber());
			List<AppVersion> versionInfo = appVersionService.getAppVersionInfo(params);
			resMap.put("versionInfo", versionInfo);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		}catch(Exception e){
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		
		this.logAfter(logger);
		return resMap;
	}
	
	@RequestMapping(value = "curVersion", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> curVersionInfo(String appType){
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("appType", appType);
			params.put("start", 0);
			params.put("showNumber", 1);
			List<AppVersion> versionInfo = appVersionService.getAppVersionInfo(params);
			resMap.put("versionInfo", versionInfo.get(0));
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		}catch(Exception e){
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		
		this.logAfter(logger);
		
		return resMap;
	}
}
